import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.awt.image.MemoryImageSource;
import java.awt.image.PixelGrabber;
import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.util.Iterator;

public class MainApp {
    public MainApp() {
        // TODO Auto-generated constructor stub
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        try {
            //createGif();
            //cut();
            //cut2();
        	test2();
        }
        catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static void createGif() throws Exception {
        // TODO Auto-generated method stub
        String outputFileName = "G:/giftt/test.gif";

        AnimatedGifEncoder e = new AnimatedGifEncoder();
        e.start(outputFileName);
        e.setDelay(1000);   // 1 frame per sec
        e.setRepeat(Short.MAX_VALUE);
        BufferedImage getImg0 = getImg(0);
        e.addFrame(getImg0);
        e.addFrame(getImg(1));
        e.addFrame(getImg(2));
        e.finish();
    }

    public static BufferedImage getImg(int idx) throws Exception {
        BufferedImage image = ImageIO.read(new File("G:/giftt/" + idx + ".jpeg"));

        Graphics2D g2d = image.createGraphics();
        g2d.setFont(HNFont.getDefaultFont());
        g2d.fillOval(2, 2, 30, 30);
        g2d.setColor(Color.RED);
        g2d.drawString("AAA" + idx, 2, 40);

        return image;
    }

    public static void saveImg(byte[] rawImageBytes) throws Exception {
        BufferedImage image = ImageIO.read (new ByteArrayInputStream(rawImageBytes));
        ImageIO.write(image, "jpg", new File("G:/giftt/snap.jpg"));
    }

    // not avail
    public static void  cut() throws  Exception {
        FileInputStream is = null;
        ImageInputStream iis = null;

         try {
            is = new  FileInputStream("g:/giftt/uiuiui.jpg");
            Iterator<ImageReader> it = ImageIO.getImageReadersByFormatName("jpeg");
            ImageReader reader = it.next();
            iis = ImageIO.createImageInputStream(is);
            reader.setInput(iis, true ) ;

            ImageReadParam param = reader.getDefaultReadParam();
            Rectangle rect =  new  Rectangle(0,0,100,100);
            param.setSourceRegion(rect);
            BufferedImage bi = reader.read( 0 ,param);

            ImageIO.write(bi, " jpg " , new  File("g:/giftt/testcut.jpeg"));
        }
        finally {
            if(is != null) {
               is.close();
            }

            if(iis != null) {
               iis.close();
            }
        }
    }

    public static void  cut2()  throws  Exception {
        File imgFile  = new File("G:/giftt/uiuiui.jpg");
        //准备从image中剪出一块图像
        Rectangle rect = new Rectangle(100, 100, 100, 200);
        BufferedImage outImg = cut3(imgFile, rect);
        ImageIO.write(outImg, "jpg", new File("g:/giftt/snap.jpg"));
    }
    
    public static BufferedImage cut4(String imgFilePath, Rectangle rect) throws Exception {
    	File imgFile  = new File(imgFilePath);
    	return cut3(imgFile, rect);
    }
        
    public static BufferedImage cut5(BufferedImage inImg, Rectangle rect) throws Exception {
    	PixelGrabber pixGrab = null; //创建像素抓取器
        MemoryImageSource imgProducer = null; //创建内存图像源
        Toolkit tool = Toolkit.getDefaultToolkit();

        int x = rect.x;
        int y = rect.y;
        int w = rect.width;
        int h = rect.height;

        int pixels[]= new int[w*h];
        pixGrab = new PixelGrabber(inImg, x, y, w, h, pixels, 0, w);
        pixGrab.grabPixels(); //开始图像的生产（抓取像素并存入pixels）
        imgProducer = new MemoryImageSource(w, h, pixels, 0, w);
        Image blockImage = tool.createImage(imgProducer);

        BufferedImage outImg = new BufferedImage(w, h, BufferedImage.SCALE_DEFAULT );
        Graphics outGraphic = outImg.createGraphics();
        outGraphic.drawImage(blockImage, 0, 0, null);

        return outImg;
    }
    
    public static BufferedImage cut3(File imgFile, Rectangle rect) throws Exception {
    	BufferedImage inImg = ImageIO.read(imgFile);
    	return cut5(inImg, rect);
    }
    
    public static BufferedImage mergeImg(Dimension rawImgDim, Image img1, Point p1, 
    		Image img2, Point p2) {
    	int w = rawImgDim.width;
        int h = rawImgDim.height;
    	BufferedImage outImg = new BufferedImage(w, h, BufferedImage.SCALE_DEFAULT );
        Graphics outGraphic = outImg.createGraphics();

        drawImg(outGraphic, img1, p1);
        drawImg(outGraphic, img2, p2);
        
        return outImg;
    }
    
    public static void test2() throws Exception {
    	BufferedImage img1 = ImageIO.read(new File("G:/giftt/0.jpeg"));
    	Point p1 = new Point(0, 0);
    	BufferedImage img2 = ImageIO.read(new File("G:/giftt/1.jpeg"));
    	Point p2 = new Point(48, 48);
    	Dimension rawImgDim = new Dimension(96, 96);
    	
    	BufferedImage aa = mergeImg(rawImgDim, img1, p1, img2, p2);
    	ImageIO.write(aa, "jpg", new File("G:/giftt/merge.jpg"));
    }

    private static void drawImg(Graphics g, Image img,  Point p) {
    	g.drawImage(img, p.x, p.y, null);
    }
}

